package com.culturesalon.dao;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.culturesalon.connectionfactory.ConnectionFactory;
import com.culturesalon.dto.Manager;
import com.culturesalon.dto.User;

public class ManagerDAOImple implements ManagerDAO {

	@Override
	public Manager get(Integer id) {
		PreparedStatement stmt=null;
		ResultSet rs=null;
		StringBuffer sql=new StringBuffer();
		sql.append(" SELECT id,userName,sex,number,password,email,phone,qq,weixin");
		
		sql.append(" FROM T_MANAGER ");
		sql.append(" WHERE ID=? ");
		Connection conn = ConnectionFactory.getConnection();
		Manager manager=null;
		try {
			stmt=conn.prepareStatement(sql.toString());
			stmt.setInt(1, id);
			rs=stmt.executeQuery();
			if(rs.next()){
				manager=new Manager();
				manager.setId(rs.getInt("id"));
				manager.setUserName(rs.getString("userName"));
				manager.setSex(rs.getString("sex"));
				manager.setNumber(rs.getString("number"));
				manager.setPassword(rs.getString("password"));
				manager.setEmail(rs.getString("email"));
				manager.setPhone(rs.getString("phone"));
				manager.setQq(rs.getString("qq"));
				manager.setWeixin(rs.getString("weixin"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally{
			ConnectionFactory.close(conn, rs, stmt);
		}
		return manager;
	}
	@Override
	public Serializable save(Manager manager) {
		Connection conn=null;
		PreparedStatement pstm=null;
		StringBuffer sql=new StringBuffer();
		sql.append(" INSERT INTO T_MANAGER( ");
		sql.append("userName,sex,number,password,email,");
		sql.append("phone,qq,weixin)");
		
		sql.append(" VALUES(?,?,?,?,?,?,?,?) ");
		conn=ConnectionFactory.getConnection();
		int result=0;
		try {
			pstm=conn.prepareStatement(sql.toString());
			pstm.setString(1, manager.getUserName());
			pstm.setString(2, manager.getSex());
			pstm.setString(3, manager.getNumber());
			pstm.setString(4, manager.getPassword());
			
			pstm.setString(5, manager.getEmail());
			pstm.setString(6, manager.getPhone());
			pstm.setString(7, manager.getQq());
			pstm.setString(8, manager.getWeixin());
			result=pstm.executeUpdate();
			return result;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}finally{
			ConnectionFactory.close(conn, null, pstm);
		}
		
		
		

	}

	@Override
	public void update(Manager manager) {
		
		Connection conn=null;
		PreparedStatement pstm=null;
		StringBuffer sql=new StringBuffer();
		sql.append(" UPDATE T_MANAGER"
				+ " SET USERNAME=( ");
        sql.append("userName=?,sex=?,number=?,password=?,email=?,phone=?,qq=?,weixin=?");
		sql.append(" VALUES(?,?,?,?,?,?,?,?) ");
		sql.append(" WHERE ID=? ");
		try {
			conn=ConnectionFactory.getConnection();
			pstm=conn.prepareStatement(sql.toString());
			pstm.setString(1, manager.getUserName());
			pstm.setString(2, manager.getSex());
			pstm.setString(3, manager.getNumber());
			pstm.setString(4, manager.getPassword());
			
			pstm.setString(5, manager.getEmail());
			pstm.setString(6, manager.getPhone());
			pstm.setString(7, manager.getQq());
			pstm.setString(8, manager.getWeixin());
			pstm.setInt(9,manager.getId());
			pstm.executeUpdate();
		
	}catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally{
		ConnectionFactory.close(conn, null, pstm);
	}
	
	

}


	@Override
	public Serializable delete(Serializable id) {
		Connection conn=null;
		PreparedStatement pstm=null;
		int result=0;
		StringBuffer sql=new StringBuffer();
		sql.append(" DELETE FROM T_MANAGER WHERE ID=? ");
		conn= ConnectionFactory.getConnection();
		try {
			pstm=conn.prepareStatement(sql.toString());
			pstm.setObject(1, id);
			result=pstm.executeUpdate();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result=-1;
		}finally{
			ConnectionFactory.close(conn, null, pstm);
		}
		
		return result;
	}

	@Override
	public List<Manager> findAll() {
		List<Manager> list=new ArrayList<Manager>();
		Connection conn=null;
		PreparedStatement pstm=null;
		ResultSet rs=null;
		StringBuffer sql=new StringBuffer();
		sql.append(" SELECT * FROM T_USER ");
		conn=ConnectionFactory.getConnection();
		Manager manager=null;
		try {
			pstm=conn.prepareStatement(sql.toString());
			rs=pstm.executeQuery();
			while(rs.next()){
				manager=new Manager();
				manager.setId(rs.getInt("id"));
				manager.setUserName(rs.getString("userName"));
				manager.setNumber(rs.getString("number"));
				manager.setSex(rs.getString("sex"));
				manager.setPassword(rs.getString("password"));
				manager.setPhone(rs.getString("phone"));
				manager.setEmail(rs.getString("email"));
				manager.setQq(rs.getString("qq"));
				manager.setWeixin(rs.getString("weixin"));
				list.add(manager);
				
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally{
			ConnectionFactory.close(conn, rs, pstm);
			
		}

	}
	@Override
	public Manager findManagerByNameAndPassword(String ManagerName, String password) {
		Connection conn=null;
		PreparedStatement pstm=null;
		ResultSet rs=null;
		Manager manager=null;
		StringBuffer sql=new StringBuffer();
		sql.append(" SELECT * FROM T_MANAGER WHERE USERNAME=? AND PASSWORD=? ");
		conn=ConnectionFactory.getConnection();
		try {
			pstm=conn.prepareStatement(sql.toString());
			pstm.setString(1, ManagerName);
			pstm.setString(2, password);
			rs=pstm.executeQuery();
			if(rs.next()){
				manager=new Manager();
				manager.setId(rs.getInt("id"));
				manager.setUserName(rs.getString("userName"));
				manager.setNumber(rs.getString("number"));
				manager.setSex(rs.getString("sex"));
				manager.setPassword(rs.getString("password"));
				manager.setPhone(rs.getString("phone"));
				manager.setEmail(rs.getString("email"));
				manager.setQq(rs.getString("qq"));
				manager.setWeixin(rs.getString("weixin"));
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			manager=null;
		}finally{
			ConnectionFactory.close(conn, rs, pstm);
		}
		
		return manager;
	}
	@Override
	public Manager findManagerByName(String managerName) {
		Connection conn=null;
		PreparedStatement pstm=null;
		ResultSet rs=null;
		Manager manager=null;
		StringBuffer sql=new StringBuffer();
		sql.append(" SELECT * FROM T_MANAGER WHERE USERNAME=? ");
		conn=ConnectionFactory.getConnection();
		try {
			pstm=conn.prepareStatement(sql.toString());
			pstm.setString(1, managerName);
			rs=pstm.executeQuery();
			if(rs.next()){
				manager=new Manager();
				manager.setId(rs.getInt("id"));
				manager.setUserName(rs.getString("userName"));
				manager.setNumber(rs.getString("number"));
				manager.setSex(rs.getString("sex"));
				manager.setPassword(rs.getString("password"));
				manager.setPhone(rs.getString("phone"));
				manager.setEmail(rs.getString("email"));
				manager.setQq(rs.getString("qq"));
				manager.setWeixin(rs.getString("weixin"));
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			manager=null;
		}finally{
			ConnectionFactory.close(conn, rs, pstm);
		}
		
		return manager;
	}

}
